#
#    Pixel War (Cliente) - Cliente de Juego Multiplayer Online
#    Copyright (C) 2010 - Francisco Rivera
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

import pygame
from pygame import *
from protocolo import Protocolo
from bloqueos import BloqueoMov
from tools import JThread

class TecladoHandler(JThread):

    def __init__(self, pantalla):
        JThread.__init__(self)
        self.pantalla = pantalla
        self.enabled = False
        self.prot = Protocolo()
        
    def activar(self):
        self.enabled = True
        self.start()
        
    def run(self):
        while True:
            tecla = pygame.key.get_pressed()
            if tecla[K_UP]:
                if not BloqueoMov.block:
                    BloqueoMov()
                    self.prot.moverNorte()
            elif tecla[K_DOWN]:
                if not BloqueoMov.block:
                    BloqueoMov()
                    self.prot.moverSur()
            elif tecla[K_LEFT]:
                if not BloqueoMov.block:
                    BloqueoMov()
                    self.prot.moverOeste()
            elif tecla[K_RIGHT]:
                if not BloqueoMov.block:
                    BloqueoMov()
                    self.prot.moverEste()
            elif tecla[K_F1]:
                self.prot.restartRound()
            elif tecla[K_ESCAPE]:
                # esta funcion corta el loop principal del juego (JThread)
                self.cortar()
                break
            pygame.time.wait(50)